 <!-- 维修 -->
 <template>
  <div class="repair-container">
    <TitleBar title="条件筛选"></TitleBar>
    <ConditionalFilter @headleSubmit="headleSubmit"></ConditionalFilter>
    <TitleBar title="报修记录"></TitleBar>
    <Record
      :isHandles="props.isHandle"
      :recordData="recordRef"
      :total="totalRef"
      :page="pageRef"
      :isLoading="isLoadingRef"
      @submitForm="submitForm"
      @currentChange="currentChange"
    ></Record>
  </div>
</template>
  
  <script setup>
import TitleBar from "@/components/TitleBar/index.vue";
import ConditionalFilter from "./components/ConditionalFilter.vue";
import { getMainRecords } from "@/api/service.js";
import { ref, watchEffect } from "vue";
import Record from "./components/Record.vue";
import { useUserStore } from "@/store/useUserStore.js";
import { storeToRefs } from "pinia";
import moment from "moment";
const isLoadingRef = ref(false)
const props = defineProps({
  isHandle: {
    type: Number,
    required: true,
  },
});
const { readUserInfo } = storeToRefs(useUserStore());
const recordRef = ref([]); // 记录的远程数据
const totalRef = ref(0); // 总数

const conditionRef = ref({
    startTime: "",
  endTime: "",
  dormitroyId:readUserInfo.value.manageArr.length !== 0
      ? readUserInfo.value.manageArr.map((item) => {
          return item.id;
        })
      : "",
  floorId: "",
  roomId: "",
  isHandle: props.isHandle,
  limit: 10,
}); // 条件对象
const pageRef = ref(1); // 页码

// 条件对象
const headleSubmit = (data) => {
    conditionRef.value.startTime = data.StartTime ? +moment(data.StartTime).utc() : ""
    conditionRef.value.endTime = data.EndTime ? +moment(data.EndTime).utc() : ""
    conditionRef.value.dormitroyId = data.dormitoryData
    conditionRef.value.floorId = data.floorData
    conditionRef.value.roomId = data.roomData
    pageRef.value = 1;
};

const currentChange = (data) => {
  pageRef.value = data;
};
// 获取记录的远程数据
watchEffect(async () => {
  isLoadingRef.value = true
  if (readUserInfo.value.manageArr.length === 0) {
    return;
  }
  if(!conditionRef.value.dormitroyId){
    conditionRef.value.dormitroyId =  readUserInfo.value.manageArr.length !== 0
      ? readUserInfo.value.manageArr.map((item) => {
          return item.id;
        })
      : ""
  }
  if (typeof conditionRef.value.dormitroyId !== "object") {
      conditionRef.value.dormitroyId = [conditionRef.value.dormitroyId]
  }
  const { data } = await getMainRecords({
    ...conditionRef.value,
    page: pageRef.value,
  });
  recordRef.value = data.rows;
  totalRef.value = data.count;
  isLoadingRef.value = false
});

const submitForm = async () => {
  isLoadingRef.value = true
  pageRef.value = 1;
  const { data } = await getMainRecords({
    ...conditionRef.value,
    page: pageRef.value,
  });
  recordRef.value = data.rows;
  totalRef.value = data.count;
  isLoadingRef.value = false
}
</script>
  
  <style lang="less" scoped>
.repair-container {
  width: 100%;
}
</style>